# -*- coding: utf-8 -*-
import logging
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext

"""
登陆页面及首页管理方法
"""

logger = logging.getLogger(__name__)


@login_required
def index(request):
	"""
	系统进入主页方法
	"""
	return render_to_response('index.html', locals(), context_instance=RequestContext(request))


def user_login(request):
	"""
	系统登陆页面
	"""
	if request.method == 'POST':
		username = request.POST['username']
		password = request.POST['password']
		user = authenticate(username=username, password=password)
		if user:
			login(request, user)
			return HttpResponseRedirect(reverse('index'))
		else:
			messages.error(request, "用户名或者密码不正确，请重新输入")
			return render_to_response('login.html', locals(), context_instance=RequestContext(request))
	else :
		return render_to_response('login.html', locals(), context_instance=RequestContext(request))